<?php

namespace app\api\logic\user;

use think\Db;
use think\facade\Request;
use app\common\access\Book as BookAccess;
use app\api\validate\Book as BookValidate;
use app\common\repository\book\C as BookCRepository;
use app\common\repository\book\U as BookURepository;
use app\common\repository\book\R as BookRRepository;
use app\common\repository\book\D as BookDRepository;

class UserBook
{
    public function __construct(
        BookAccess $bookAccess,
        BookCRepository $bookCRepository,
        BookURepository $bookURepository,
        BookRRepository $bookRRepository,
        BookDRepository $bookDRepository
    ) {
        $this->bookAccess = $bookAccess;
        $this->bookCRepository = $bookCRepository;
        $this->bookURepository = $bookURepository;
        $this->bookRRepository = $bookRRepository;
        $this->bookDRepository = $bookDRepository;
    }

    public function create()
    {
        $validate = new BookValidate;
        $bookEntity = ['user_id' => request()->user->id, 'title' => Request::param('title'), 'desc' => Request::param('desc'), 'cover' => Request::param('cover')];
        $result = $validate->scene('create')->check($bookEntity);
        if ($result !== true) {
            return ['done' => false, 'code' => config('code.common.paramsRequire')];
        }
        Db::startTrans();
        try {
            $article = $this->bookCRepository->create($bookEntity);
            $section = Request::param('section');
            if (!empty($section)) {
                $article->section()->saveAll($section);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }

    public function update()
    {
        $validate = new BookValidate;
        $bookId = Request::param('id');
        $this->bookAccess->check($bookId);
        $bookEntity = ['id' => $bookId, 'title' => Request::param('title'), 'desc' => Request::param('desc'), 'cover' => Request::param('cover')];
        $result = $validate->scene('update')->check($bookEntity);
        if ($result !== true) {
            return ['done' => false, 'code' => config('code.common.paramsRequire')];
        }
        Db::startTrans();
        try {
            $this->bookURepository->updateBook($bookEntity, ['id' => $bookId]);
            $section = Request::param('section');
            if (!empty($section)) {
                $this->bookURepository->updateBookSection($section, $bookId);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }

    public function get()
    {
        $bookId = Request::param('id');
        $this->bookAccess->check($bookId);
        if (!$bookId) {
            return ['done' => false, 'code' => config('code.common.paramsRequire')];
        }
        return ['data' => $this->bookRRepository->getBook(['id' => $bookId])];
    }

    public function getBooks()
    {
        $pageIndex = Request::param('index');
        $pageSize = Request::param('size');
        if (!$pageIndex || !$pageSize) {
            return ['done' => false, 'code' => config('code.common.paramsRequire')];
        }
        $pageData = $this->bookRRepository->getBooksWithPage(['user_id' => request()->user->id], [], ['index' => $pageIndex, 'size' => $pageSize]);
        return ['data' => $pageData['list'], 'page' => ['index' => $pageIndex, 'size' => $pageSize, 'count' => $pageData['count']]];
    }

    public function getOwnBooks()
    {
        $books = $this->bookRRepository->getBooks(['user_id' => request()->user->id]);
        return ['data' => $books];
    }

    public function delete()
    {
        $this->bookDRepository->delete(Request::param('id'));
    }
}
